package com.jbh.wanandroidkotlin.fragment

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.jbh.apilibrary.AppData
import com.jbh.apilibrary.bean.ChildrenData
import com.jbh.apilibrary.bean.IndexArticleListBean
import com.jbh.apilibrary.bean.TreeResult
import com.jbh.apilibrary.i.ApiCallBack
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.activity.ArticleListActivity
import com.jbh.wanandroidkotlin.activity.CommonActivity
import com.jbh.wanandroidkotlin.util.WEB_LINK
import com.jbh.wanandroidkotlin.util.data
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout
import kotlinx.android.synthetic.main.fragment_item_t_x.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class ItemTXFragment : Fragment() {

    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_item_t_x, container, false)
    }


    companion object {
        @JvmStatic
        fun newInstance(param1: String ="", param2: String ="") =
            ItemTXFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mRecyclerView.apply {
            layoutManager = LinearLayoutManager(activity)
        }

        when(param1){
            "1" ->{ mainTreeTask()}
            "2" ->{ mainNaviTask()}
        }
    }




    private fun mainTreeTask(){
        AppData.instance().mainTreeTask(object : ApiCallBack<List<TreeResult>> {
            override fun success(data: List<TreeResult>) {
                println("[mainTreeTask] - >success= ${Gson().toJson(data)}")
                activity?.runOnUiThread {
                    mRecyclerView.adapter = TXDataAdapter(data)
                }
            }
            override fun error(error: String) {
                println("[mainTreeTask] - > error = $error")
            }
        })
    }


    private fun mainNaviTask(){
        AppData.instance().mainNaviTask(object : ApiCallBack<List<TreeResult>> {
            override fun success(data: List<TreeResult>) {
                println("[mainNaviTask] - >success= ${Gson().toJson(data)}")
                activity?.runOnUiThread {
                    mRecyclerView.adapter = TXDataAdapter(data)
                }
            }
            override fun error(error: String) {
                println("[mainNaviTask] - > error = $error")
            }
        })
    }



   inner class TXDataAdapter(data: List<TreeResult>)
       : BaseQuickAdapter<TreeResult,BaseViewHolder>(R.layout.item_tx_layout,data){
        override fun convert(helper: BaseViewHolder, item: TreeResult) {
            helper.setText(R.id.title,item.name)
            var mTagFlowLayout = helper.getView<TagFlowLayout>(R.id.mTagFlowLayout)

            when(param1){
                "1" ->{ itemTXTask(mTagFlowLayout,item)}
                "2" ->{ itemDHTask(mTagFlowLayout,item)}
            }


        }
    }

    private fun itemDHTask(mTagFlowLayout: TagFlowLayout, item: TreeResult) {
        mTagFlowLayout.apply {
            adapter = object :TagAdapter<IndexArticleListBean>(item.articles){
                override fun getView(
                    parent: FlowLayout?,
                    position: Int,
                    t: IndexArticleListBean?
                ): View {
                    return TextView(activity).apply {
                        text = t?.title
                        textSize = 12F;
                        setPadding(20,10,20,10)
                        setTextColor(Color.WHITE)
                        background = getDrawableBg(Color.GRAY,100f)

                        setOnClickListener {
                            val b = Bundle();
                            b.putString(WEB_LINK,t?.link)
                            activity!!.startActivity(Intent(activity,CommonActivity::class.java)
                                .putExtra(data, b))
                        }
                    }
                }
            }
        }
    }


    /**
     * 体系
     *
     */
    private fun itemTXTask(mTagFlowLayout: TagFlowLayout, item: TreeResult) {
        mTagFlowLayout.apply {
            adapter = object :TagAdapter<ChildrenData>(item.children){
                override fun getView(
                    parent: FlowLayout?,
                    position: Int,
                    t: ChildrenData?
                ): View {
                    return TextView(activity).apply {
                        text = t?.name
                        textSize = 12F;
                        setPadding(20,10,20,10)
                        setTextColor(Color.WHITE)
                        background = getDrawableBg(Color.GRAY,100f)

                        setOnClickListener {
                            activity!!.startActivity(Intent(activity,ArticleListActivity::class.java)
                                .putExtra("id",t!!.id))
                        }
                    }
                }
            }
        }
    }


    private fun getDrawableBg(color:Int,r:Float): GradientDrawable = GradientDrawable().apply {
        setColor(color)
        cornerRadius = r
    }



}